package com.quanyan.assets.service.impl;


import com.quanyan.api.APIRequest;
import com.quanyan.api.APIResponse;
import com.quanyan.api.enums.ApiResponseEnum;
import com.quanyan.api.enums.AssetsType;
import com.quanyan.assets.emun.OpType;
import com.quanyan.assets.entity.TbUserEggsAccumulateHistoryExample;
import com.quanyan.assets.entity.TbUserShellAccumulateHistoryExample;
import com.quanyan.assets.mapper.ETbUserEggsAccumulateHistoryMapper;
import com.quanyan.assets.mapper.ETbUserShellAccumulateHistoryMapper;
import com.quanyan.assets.mapper.TbUserEggsAccumulateHistoryMapper;
import com.quanyan.assets.mapper.TbUserShellAccumulateHistoryMapper;
import com.quanyan.assets.request.ReqUserAccumulateHistory;
import com.quanyan.assets.response.RespAdminUserAccumulateHistory;
import com.quanyan.assets.response.RespUserAccumulateHistory;
import com.quanyan.assets.service.UserAccumulateHistoryService;
import com.quanyan.common.Plugin.Page;
import com.quanyan.common.Plugin.PageObj;
import com.quanyan.common.exception.BizException;
import com.quanyan.common.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Valid;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by wmb on 2016/6/2.
 */
@Service("userAccumulateHistoryService")
public class UserAccumulateHistoryServiceImpl implements UserAccumulateHistoryService {
    @Autowired
    private ETbUserEggsAccumulateHistoryMapper eTbUserEggsAccumulateHistoryMapper;
    @Autowired
    private ETbUserShellAccumulateHistoryMapper eTbUserShellAccumulateHistoryMapper;
    @Autowired
    private TbUserEggsAccumulateHistoryMapper tbUserEggsAccumulateHistoryMapper;
    @Autowired
    private TbUserShellAccumulateHistoryMapper tbUserShellAccumulateHistoryMapper;
    @Override
    @Valid
    public APIResponse<PageObj<List<RespUserAccumulateHistory>>> getUserAccumulateHistory(APIRequest<ReqUserAccumulateHistory> apiRequest) throws ParseException {
        ReqUserAccumulateHistory reqUserAccumulateHistory=apiRequest.getData();
        if(reqUserAccumulateHistory.getAssetsType()== AssetsType.ASSETS_EGGS.id.byteValue()){
//            //获取鸟蛋历史查询条件
//            TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=getEggsExample(reqUserAccumulateHistory);
//            //获取总数
//            int count=tbUserEggsAccumulateHistoryMapper.countByExample(tbUserEggsAccumulateHistoryExample);
            int count=eTbUserEggsAccumulateHistoryMapper.countUserEggsAccumulateHistory(reqUserAccumulateHistory);
            Page page = Page.createPage(count, reqUserAccumulateHistory.getPageNo(), reqUserAccumulateHistory.getPageSize());
            reqUserAccumulateHistory.setPageNo(reqUserAccumulateHistory.getPageNo()*reqUserAccumulateHistory.getPageSize());
            //设置分页
            List<RespUserAccumulateHistory> userEggsAccumulateHistoryList=eTbUserEggsAccumulateHistoryMapper.getUserEggsAccumulateHistory(reqUserAccumulateHistory);
            PageObj<List<RespUserAccumulateHistory>> pageObj = PageObj.create(count, reqUserAccumulateHistory.getPageNo()/reqUserAccumulateHistory.getPageSize(), reqUserAccumulateHistory.getPageSize(), userEggsAccumulateHistoryList);
            return APIResponse.returnSuccess(pageObj);
        }else if(reqUserAccumulateHistory.getAssetsType()== AssetsType.ASSETS_SHELL.id.byteValue()){
            TbUserShellAccumulateHistoryExample tbUserShellAccumulateHistoryExample=getShellExample(reqUserAccumulateHistory);
            int count=tbUserShellAccumulateHistoryMapper.countByExample(tbUserShellAccumulateHistoryExample);
            Page page = Page.createPage(count, reqUserAccumulateHistory.getPageNo(), reqUserAccumulateHistory.getPageSize());
            //设置分页
            List<RespUserAccumulateHistory> userShellAccumulateHistoryList=eTbUserShellAccumulateHistoryMapper.getUserShellAccumulateHistory(reqUserAccumulateHistory);
            PageObj<List<RespUserAccumulateHistory>> pageObj = PageObj.create(count, reqUserAccumulateHistory.getPageNo()/reqUserAccumulateHistory.getPageSize(), reqUserAccumulateHistory.getPageSize(), userShellAccumulateHistoryList);
            RespAdminUserAccumulateHistory respAdminUserAccumulateHistory = new RespAdminUserAccumulateHistory();
            respAdminUserAccumulateHistory.setPageObj(pageObj);
            return APIResponse.returnSuccess(pageObj);
        }else{
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID);
        }


    }

    @Override
    @Valid
    public APIResponse<RespAdminUserAccumulateHistory> getAdminUserAccumulateHistory(APIRequest<ReqUserAccumulateHistory> apiRequest) throws ParseException {
        ReqUserAccumulateHistory reqUserAccumulateHistory=apiRequest.getData();
        if(reqUserAccumulateHistory.getAssetsType()== AssetsType.ASSETS_EGGS.id.byteValue()){
//            //获取鸟蛋历史查询条件
//            TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=getEggsExample(reqUserAccumulateHistory);
//            //获取总数
//            int count=tbUserEggsAccumulateHistoryMapper.countByExample(tbUserEggsAccumulateHistoryExample);
            int count=eTbUserEggsAccumulateHistoryMapper.countUserEggsAccumulateHistory(reqUserAccumulateHistory);
            Page page = Page.createPage(count, reqUserAccumulateHistory.getPageNo(), reqUserAccumulateHistory.getPageSize());
            reqUserAccumulateHistory.setPageNo(reqUserAccumulateHistory.getPageNo()*reqUserAccumulateHistory.getPageSize());
            //设置分页
            List<RespUserAccumulateHistory> userEggsAccumulateHistoryList=eTbUserEggsAccumulateHistoryMapper.getUserEggsAccumulateHistory(reqUserAccumulateHistory);
            PageObj<List<RespUserAccumulateHistory>> pageObj = PageObj.create(count, reqUserAccumulateHistory.getPageNo()/reqUserAccumulateHistory.getPageSize(), reqUserAccumulateHistory.getPageSize(), userEggsAccumulateHistoryList);
            RespAdminUserAccumulateHistory respAdminUserAccumulateHistory = new RespAdminUserAccumulateHistory();
            respAdminUserAccumulateHistory.setPageObj(pageObj);
            Long sumAccumulate = 0L;
            if(null != reqUserAccumulateHistory.getOpType()){
                sumAccumulate = (reqUserAccumulateHistory.getOpType() ==0?-1:1)*eTbUserEggsAccumulateHistoryMapper.getSumAccumulate(reqUserAccumulateHistory);
            }else {
                reqUserAccumulateHistory.setOpType(OpType.CONSUME.id.byteValue());
                Long consumeAccumulate = -1*eTbUserEggsAccumulateHistoryMapper.getSumAccumulate(reqUserAccumulateHistory);
                reqUserAccumulateHistory.setOpType(OpType.RECEIVE.id.byteValue());
                Long receiveAccumulate = eTbUserEggsAccumulateHistoryMapper.getSumAccumulate(reqUserAccumulateHistory);
                sumAccumulate = consumeAccumulate + receiveAccumulate;
            }
            respAdminUserAccumulateHistory.setSumAccumulate(sumAccumulate);
            return APIResponse.returnSuccess(respAdminUserAccumulateHistory);
        }else if(reqUserAccumulateHistory.getAssetsType()== AssetsType.ASSETS_SHELL.id.byteValue()){
            TbUserShellAccumulateHistoryExample tbUserShellAccumulateHistoryExample=getShellExample(reqUserAccumulateHistory);
            int count=tbUserShellAccumulateHistoryMapper.countByExample(tbUserShellAccumulateHistoryExample);
            Page page = Page.createPage(count, reqUserAccumulateHistory.getPageNo(), reqUserAccumulateHistory.getPageSize());
            //设置分页
            List<RespUserAccumulateHistory> userShellAccumulateHistoryList=eTbUserShellAccumulateHistoryMapper.getUserShellAccumulateHistory(reqUserAccumulateHistory);
            PageObj<List<RespUserAccumulateHistory>> pageObj = PageObj.create(count, reqUserAccumulateHistory.getPageNo()/reqUserAccumulateHistory.getPageSize(), reqUserAccumulateHistory.getPageSize(), userShellAccumulateHistoryList);
            RespAdminUserAccumulateHistory respAdminUserAccumulateHistory = new RespAdminUserAccumulateHistory();
            respAdminUserAccumulateHistory.setPageObj(pageObj);
            return APIResponse.returnSuccess(respAdminUserAccumulateHistory);
        }else{
            throw new BizException(ApiResponseEnum.PARAMETER_INVALID);
        }


    }



    private TbUserShellAccumulateHistoryExample getShellExample(ReqUserAccumulateHistory reqUserAccumulateHistory) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TbUserShellAccumulateHistoryExample tbUserShellAccumulateHistoryExample=new TbUserShellAccumulateHistoryExample();
        TbUserShellAccumulateHistoryExample.Criteria criteria=tbUserShellAccumulateHistoryExample.createCriteria();
        if(null!=reqUserAccumulateHistory.getUid()){
            criteria.andUidEqualTo(reqUserAccumulateHistory.getUid());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getSystemCode())){
            criteria.andSystemCodeEqualTo(reqUserAccumulateHistory.getSystemCode());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getBusinessNo())){
            criteria.andBusinessNoEqualTo(reqUserAccumulateHistory.getBusinessNo());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getBusinessCode())){
            criteria.andBusinessCodeEqualTo(reqUserAccumulateHistory.getBusinessCode());
        }
        if(null!=reqUserAccumulateHistory.getOpType()){
            criteria.andOpTypeEqualTo(reqUserAccumulateHistory.getOpType());
        }
        if(null!=reqUserAccumulateHistory.getMinAccumulate()){
            criteria.andAccumulateGreaterThanOrEqualTo(reqUserAccumulateHistory.getMinAccumulate());
        }
        if(null!=reqUserAccumulateHistory.getMaxAccumulate()) {
            criteria.andAccumulateLessThanOrEqualTo(reqUserAccumulateHistory.getMaxAccumulate());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getStartCreateTime())){
            criteria.andCreateTimeGreaterThanOrEqualTo(sdf.parse(reqUserAccumulateHistory.getStartCreateTime()));
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getEndCreateTime())){
            criteria.andCreateTimeLessThanOrEqualTo(sdf.parse(reqUserAccumulateHistory.getEndCreateTime()));
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getSerialNumber())){
            criteria.andSerialNumberEqualTo(reqUserAccumulateHistory.getSerialNumber());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getPayReceive())){
            criteria.andPayReceiveEqualTo(reqUserAccumulateHistory.getPayReceive());
        }
        if(null!=reqUserAccumulateHistory.getStatusList()&&!reqUserAccumulateHistory.getStatusList().isEmpty()){
            criteria.andStatusIn(reqUserAccumulateHistory.getStatusList());
        }
        return tbUserShellAccumulateHistoryExample;
    }

    private TbUserEggsAccumulateHistoryExample getEggsExample(ReqUserAccumulateHistory reqUserAccumulateHistory) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        TbUserEggsAccumulateHistoryExample tbUserEggsAccumulateHistoryExample=new TbUserEggsAccumulateHistoryExample();
        TbUserEggsAccumulateHistoryExample.Criteria criteria=tbUserEggsAccumulateHistoryExample.createCriteria();
        if(null!=reqUserAccumulateHistory.getUid()){
            criteria.andUidEqualTo(reqUserAccumulateHistory.getUid());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getSystemCode())){
            criteria.andSystemCodeEqualTo(reqUserAccumulateHistory.getSystemCode());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getBusinessNo())){
            criteria.andBusinessNoEqualTo(reqUserAccumulateHistory.getBusinessNo());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getBusinessCode())){
            criteria.andBusinessCodeEqualTo(reqUserAccumulateHistory.getBusinessCode());
        }
        if(null!=reqUserAccumulateHistory.getOpType()){
            criteria.andOpTypeEqualTo(reqUserAccumulateHistory.getOpType());
        }
        if(null!=reqUserAccumulateHistory.getMinAccumulate()){
            criteria.andAccumulateGreaterThanOrEqualTo(reqUserAccumulateHistory.getMinAccumulate());
        }
        if(null!=reqUserAccumulateHistory.getMaxAccumulate()) {
            criteria.andAccumulateLessThanOrEqualTo(reqUserAccumulateHistory.getMaxAccumulate());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getStartCreateTime())){
            criteria.andCreateTimeGreaterThanOrEqualTo(sdf.parse(reqUserAccumulateHistory.getStartCreateTime()));
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getEndCreateTime())){
            criteria.andCreateTimeLessThanOrEqualTo(sdf.parse(reqUserAccumulateHistory.getEndCreateTime()));
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getSerialNumber())){
            criteria.andSerialNumberEqualTo(reqUserAccumulateHistory.getSerialNumber());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getPayReceive())){
            criteria.andPayReceiveEqualTo(reqUserAccumulateHistory.getPayReceive());
        }
        if(!StringUtil.isBlankOrNull(reqUserAccumulateHistory.getMobile())){
            criteria.andMobileEqualTo(reqUserAccumulateHistory.getMobile());
        }
        if(null!=reqUserAccumulateHistory.getStatusList()&&!reqUserAccumulateHistory.getStatusList().isEmpty()){
            criteria.andStatusIn(reqUserAccumulateHistory.getStatusList());
        }
        return tbUserEggsAccumulateHistoryExample;
    }


}
